package com.encryption.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.dao.Condition;
import com.encryption.dao.FileDAO;
import com.encryption.model.File;
import com.encryption.model.User;
import com.encryption.service.FileService;

/**
 * 文件权限服务接口实现类
 * 
 * @author Administrator
 * 
 */
public class FileServiceImpl implements FileService {

	private static final Logger log = LoggerFactory
			.getLogger(FileServiceImpl.class);

	private static FileServiceImpl fileServiceImpl = null;

	public static FileServiceImpl getFileServiceImpl() {
		if (null == fileServiceImpl) {
			fileServiceImpl = new FileServiceImpl();
		}
		return fileServiceImpl;
	}

	/**
	 * 根据文件ID获取文件对象
	 * 
	 * @param
	 */
	public File getById(String id) {
		return FileDAO.getFileDAO().findById(Integer.parseInt(id));
	}

	/**
	 * 根据条件获取文件
	 * 
	 * @param
	 */
	public List<File> getFileByCondition(List<Condition> condList) {
		return FileDAO.getFileDAO().findByExample(condList);
	}

	/**
	 * 根据条件分页获取文件
	 * 
	 * @param condList
	 * @return
	 */
	public List<File> getFilePagingByCondition(List<Condition> condList,
			User user, int page, int pageSize) {
		return FileDAO.getFileDAO().findPagingByCondition(user, condList, page,
				pageSize);
	}

	/**
	 * 根据条件分页获取文件
	 * 
	 * @param condList
	 * @return
	 */
	public int getFilePagingCountByCondition(List<Condition> condList, User user) {
		return FileDAO.getFileDAO().findTotalCountByCondition(user, condList);
	}

	/**
	 * 增加文件
	 * 
	 * @param file
	 * @return
	 */
	public boolean addFile(File file) {
		try {

			FileDAO.getFileDAO().save(file);
		} catch (Exception e) {
			log.error("add file error:", e);
			return false;
		}
		return true;

	}

	/**
	 * 更新文件信息
	 * 
	 * @param file
	 * @return
	 */
	public boolean updateFile(File file) {
		try {
			
			FileDAO.getFileDAO().merge(file);
		} catch (Exception e) {
			log.error("update file error:", e);
			return false;
		}
		return true;
	}

	/**
	 * 删除文件信息
	 * 
	 * @param id
	 * @return
	 */
	public boolean deleteFile(String id) {
		try {
			File file = new File();
			file.setId(Integer.parseInt(id));
			FileDAO.getFileDAO().delete(file);
		} catch (Exception e) {
			log.error("delete file error:", e);
			return false;
		}
		return true;
	}

	/**
	 * 上传文件
	 * 
	 * @param bytes
	 * @param fileName
	 * @return
	 */
	public boolean uploadView(byte[] bytes, String fileName) {
		java.io.File file = new java.io.File("/uploadFile/" + fileName);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			String context = new String(bytes, "UTF-8");
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(context);
			osw.close();
			fos.close();
		} catch (IOException e) {
			log.error("文件上传出现IO异常。文件路径：" + file.getAbsolutePath());
			return false;
		}
		return true;
	}

	@Override
	public List<File> getMyFilePagingByCondition(List<Condition> condList,
			int page, int pageSize) {
		return FileDAO.getFileDAO().findPagingByCondition(condList, page, pageSize);
	}

}
